﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Facebook.Web;
using System.Data.SqlClient;

public partial class Multimedia : LutefiskPage
{

    protected void Page_Load(object sender, EventArgs e)
    {
        // Images need to have an absolute path to be displayed!
        // Annoying, I know... but I don't make the rules        
        videoImage.ImageUrl = MasterPage.ConvertRelativeUrlToAbsoluteUrl(Page, "~/Images/uploadvideo.png");

        // Set the Current Musician's Data to the Session UID (the musician we requested to view)
        CurrentMusician = Musician.CreateMusician(Session["uid"].ToString());

        // Display any multimedia the user has
        LoadMultimedia(CurrentMusician.UID);


        // Set text && images based off of CurrentMusician data.
        multimediaHeaderLabel.Text = multimediaHeaderLabel.Text.Replace("XXX", CurrentMusician.Name);
        multimediaDescriptionLabel.Text = multimediaDescriptionLabel.Text.Replace("XXX", CurrentMusician.Name);
        noMultimediaDescriptionLabel.Text = noMultimediaDescriptionLabel.Text.Replace("XXX", CurrentMusician.Name);
        profileImage.ImageUrl = CurrentMusician.Picture;
        profileLabel.Text = profileLabel.Text.Replace("XXX", CurrentMusician.Name);
    }

    private void LoadMultimedia(string UID)
    {
        using (SqlConnection dbconn = new SqlConnection(Lutefisk.CONNECTION_STRING))
        {
            dbconn.Open();

            string searchString = "SELECT * FROM musicians WHERE UID=" + UID + ";";
            using (SqlCommand sql = new SqlCommand(searchString, dbconn))
            {
                // Retrieve Videos
                using (SqlDataReader reader = sql.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        List<EmbeddedObject> multimediaList = EmbeddedObject.CreateList(reader["multimedia"].ToString());

                        // Musician does have multimedia
                        if (multimediaList.Count > 1 || (multimediaList.Count == 1 && multimediaList[0].ID != ""))
                        {
                            MultimediaRepeater.DataSource = multimediaList;
                            MultimediaRepeater.DataBind();
                        }
                        // Musician does not have multimedia
                        else
                        {
                            noMultimediaPanel.Visible = true;
                            multimediaPanel.Visible = false;
                        }

                        int i = MultimediaRepeater.Items.Count;

                        multimediaMessage.Text = " Displaying " + i + " Multimedia Item" + ((i == 1) ? "" : "s");
                    }
                }
            }
        }
    }

    protected void uploadMultimediaClick(object sender, EventArgs e)
    {
        Lutefisk.DirectPage(Page.Page, "UploadMultimedia");                    //redirect the page to search
    }


    protected void profileClick(object sender, EventArgs e)
    {
        // Navigate to the current musicians profile.
        Session["uid"] =  CurrentMusician.UID;
        Lutefisk.DirectPage(Page.Page, "Profile");
    }

    protected void multimediaClick(object sender, EventArgs e)
    {
        // Navigate to the current musician's multimedia.
        Session["uid"] = CurrentMusician.UID;
        Lutefisk.DirectPage(Page.Page, "Multimedia");
    }

    protected void curriculumVitaeClick(object sender, EventArgs e)
    {
        // Navigate to the current musician's cv.
        Session["uid"] = CurrentMusician.UID;
        Lutefisk.DirectPage(Page.Page, "CV");
    }


}